package cn.jiedh.app.meiweidk.Utils.db;

import android.database.Cursor;

import cn.jiedh.app.meiweidk.Utils.tool.SPUtils;
import cn.jiedh.app.meiweidk.app.MyApp;
import cn.jiedh.app.meiweidk.entity.MsgInfoBean;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * @name：
 * @author： 周毅
 * @phone： 15826080712
 * @createTime： 2017/6/5
 * @modifyTime： 2017/6/5
 * @explain：
 */

public class Dbutils {
    DbManager db;
    

    public Dbutils() {
        db = x.getDb(MyApp.getInstance().getDaoConfig());
    }

    public List<MsgInfoBean> Getallmessage() {
        Cursor cursor = null;
        try {
            cursor = db.execQuery(" SELECT * FROM MsgInfoBean ORDER BY id DESC ");
        } catch (DbException e) {
            e.printStackTrace();
        }
        List<MsgInfoBean> list = new ArrayList<>();
        while (cursor != null && cursor.moveToNext()) {
            MsgInfoBean s = new MsgInfoBean();
            s.setId(cursor.getString(cursor.getColumnIndex("id")));
            s.setState(cursor.getString(cursor.getColumnIndex("state")));
            list.add(s);
        }
        if (cursor != null) {
            cursor.close();
        }
        return list;
    }

    public void SaveMessage(MsgInfoBean m) {
        try {
            db.saveOrUpdate(m);
        } catch (DbException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * 修改数据库
     */
    public void Updatestate(MsgInfoBean m) {
        try {
            m.setState("1");
            db.replace(m);
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    /**
     * 退出时删除表
     */
    public void dropTable() {
        try {
            db.dropTable(MsgInfoBean.class);
        } catch (DbException e) {
            e.printStackTrace();
        }
    }


    public void Savemoney(SaveMoney saveMoney){
        try {
            db.saveOrUpdate(saveMoney);
        } catch (DbException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void ADDmoney( String phone ,String money){
        SPUtils.saveToApp(MyApp.mContext,phone,money);
    }

}
